package com.shijiu.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijiu.common.Result;
import com.shijiu.dao.mapper.GoodsMapper;
import com.shijiu.pojo.Goods;
import com.shijiu.pojo.User;
import com.shijiu.service.GoodsService;
import com.shijiu.vo.GoodLisVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xlj
 * @Date Created in  2023/4/21 17:11
 * @DESCRIPTION:
 * @Version V1.0
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 通过雪花算法生成唯一id
     */
    private static final Snowflake SNOW_FLAKE = IdUtil.getSnowflake(1,1);

    @Override
    public Result saveOrUpdateById(Goods goods) {
        String id = goods.getId();
        Integer count = 0;
        //获取当前时间
        String today = DateUtil.today();
        Date dateTime = DateUtil.parse(today, "yyyy-MM-dd");
        String goodsName = goods.getName();
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getName,goodsName);
        Goods selectedOne = goodsMapper.selectOne(wrapper);
        if (selectedOne != null) {
            Integer selectedOneStores = selectedOne.getStores();
            Goods goods1 = new Goods();
            BeanUtils.copyProperties(goods,goods1, String.valueOf(goods.getStores()));
            goods1.setStores(goods.getStores()+selectedOneStores);
            goods1.setId(selectedOne.getId());
            count = goodsMapper.updateById(goods1);
            return result(count);
        }
        if (id != null) {
            //id 不为空时走编辑
            Goods goods1 = new Goods();
            BeanUtils.copyProperties(goods, goods1);
            goods1.setUpdateTime(dateTime);
            count = goodsMapper.updateById(goods1);
            return result(count);
        }
        //走新增
        //生成id
        String goodsId = SNOW_FLAKE.nextIdStr();
        Goods newGood = new Goods();
        //拷贝属性
        BeanUtils.copyProperties(goods, newGood);
        newGood.setId(goodsId);
        newGood.setIsDeleted("0");
        newGood.setShelf("0");
        newGood.setCreateTime(dateTime);
        newGood.setUpdateTime(dateTime);
        count = goodsMapper.insert(newGood);
        return result(count);
    }

    @Override
    public Result deletedById(String goodsId) {
        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setIsDeleted("1");
        int count = goodsMapper.updateById(goods);
        return result(count);
    }

    @Override
    public Result selectOrderListByPage(Map<String, Object> requestBodyMaps) {
        Integer pageCount = (Integer) requestBodyMaps.get("page");
        Integer pageSize = (Integer) requestBodyMaps.get("pageSize");
        String name = (String) requestBodyMaps.get("name");
        String unit = (String) requestBodyMaps.get("unit");
        String species = (String) requestBodyMaps.get("species");
        String user = (String) requestBodyMaps.get("user");
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果姓名不为 null 不为空，模糊查询
        if (StrUtil.isNotBlank(name)) {
            lambdaQueryWrapper.like(Goods::getName, "%" + name + "%");
        }
        //如果单位不为空查询定位信息
        if (StrUtil.isNotBlank(unit)) {
            lambdaQueryWrapper.like(Goods::getUnit, unit);
        }
        //如果商品种类不为空，查询商品种类
        if (StrUtil.isNotBlank(species)) {
            lambdaQueryWrapper.eq(Goods::getSpecies, species);
        }
        lambdaQueryWrapper.eq(Goods::getIsDeleted,"0");
        if ("admin".equals(user)){
            goodsMapper.selectList(lambdaQueryWrapper);
        }else {
            lambdaQueryWrapper.eq(Goods::getShelf, "0");
            goodsMapper.selectList(lambdaQueryWrapper);
        }

        // 查询第pageCount页，每页pageSize条记录
        Page<Goods> page = new Page<>(pageCount, pageSize);
        IPage<Goods> goodPage = goodsMapper.selectPage(page, lambdaQueryWrapper);
        // 当前页的记录列表
        List<Goods> goodList = goodPage.getRecords();
        // 总记录数
        long total = goodPage.getTotal();
        GoodLisVo goodListVo = new GoodLisVo();
        goodListVo.setGoodsVoList(goodList);
        goodListVo.setTotal(total);
        return Result.success(goodListVo);
    }

    @Override
    public Result shelfById(String goodsId) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getId,goodsId);
        Goods goods = goodsMapper.selectOne(wrapper);
        String shelf = goods.getShelf();
        Goods good = new Goods();
        good.setId(goods.getId());
        Integer count = 0;
        if (StrUtil.isNotBlank(shelf)) {
            if ("0".equals(shelf)){
                good.setShelf("1");
            }else {
                good.setShelf("0");
            }
            count = goodsMapper.updateById(good);
        }
        return result(count);
    }

    public static Result result(Integer count){
        if (count >= 1) {
            return Result.success(count);
        }
        return Result.fail(500,"操作失败，请稍后再试");
    }
}
